package class51To60;

import java.io.*;

public class RobotPassThroughBuilding {

    public static int MAX_N = 100005;

    public static int[] arr = new int[MAX_N];

    public static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            int l = 0;
            int r = 0;
            for (int i = 0; i < n; i++) {
                in.nextToken();
                arr[i] = (int) in.nval;
                r = Math.max(r, arr[i]);
            }
            out.println(compute(l, r, r));
        }
        out.flush();
        out.close();
        br.close();
    }

    public static int compute(int l, int r, int max) {
        int ans = 0;
        int m = 0;
        while (l <= r) {
            m = l + ((r - l) >> 1);
            if (check(m, max)) {
                ans = m;
                r = m - 1;
            } else {
                l = m + 1;
            }
        }
        return ans;
    }

    public static boolean check(int energy, int max) {
        for (int i = 0; i < n; i++) {
            if (energy >= arr[i]) {
                energy += energy - arr[i];
            } else {
                energy -= arr[i] - energy;
            }
            if (energy > max) {
                return true;
            }
            if (energy < 0) {
                return false;
            }
        }
        return true;
    }
}
